package com.corewillsoft.fireflies.ui;

import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.corewillsoft.fireflies.sounds.SoundsManager;
import com.corewillsoft.fireflies.storage.objects.MapLevel;
import com.corewillsoft.fireflies.ui.BaseActivity.OnMenuClickedListener;

public class MapActivity extends BaseMapActivity implements OnMenuClickedListener {

	public enum MapLevelType {
		BACKGROUND,
		LEVELS,
		NAVIGATION
	}

	private List<MapLevelTexture> mapLevelTextures = new ArrayList<MapLevelTexture>();
	private MapLevel selectedMapLevel;

	@Override
	public Scene onLoadScene() {
		getEngine().registerUpdateHandler(new FPSLogger());

		setOnMenuClickListener(this);

		scene = new Scene();
		scene.setTouchAreaBindingEnabled(true);
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
		for (int i = 0; i < MapLevelType.values().length; i++) {
			this.scene.attachChild(new Entity());
		}

		addMapLevels();
		createControlBar();
		return scene;
	}

	private void addMapLevels() {
		for (MapLevel mapLevel : dbManager.getMapLevels()) {
			MapLevelTexture levelTexture = getLevelArea(mapLevel);
			scene.getChild(MapLevelType.LEVELS.ordinal()).attachChild(levelTexture);

			boolean isMapLevelAvailable = dbManager.isMapLevelAvailable(mapLevel.getLevel());
			levelTexture.setMapLevelAvailable(isMapLevelAvailable);

			mapLevelTextures.add(levelTexture);
		}
	}

	private MapLevelTexture getLevelArea(final MapLevel mapLevel) {
		MapLevelTexture levelRectangle = new MapLevelTexture(mapLevel, getEngine(), this) {

			@Override
			public boolean onAreaTouched(final TouchEvent pTouchEvent,
					final float pX, final float pY) {
				switch (pTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					SoundsManager.getInstance().playMenuClick();
					break;
				case TouchEvent.ACTION_UP:
					if (!dbManager.isMapLevelAvailable(mapLevel.getLevel())) {
						break;
					}
					if (dbManager.isLevelIncompletedAndSaved(mapLevel.getLevel())) {
						selectedMapLevel = mapLevel;
						showMapMenu(MapActivity.this);
					} else {
						startGame(mapLevel.getLevel(), true);
					}
					break;
				default:
					break;
				}
				return true;
			}
		};
		scene.registerTouchArea(levelRectangle);
		levelRectangle.setColor(255, 0, 0);
		return levelRectangle;
	}

	@Override
	public void onResumeGame() {
		super.onResumeGame();
		for (MapLevelTexture texture : mapLevelTextures) {
			texture.setMapLevelAvailable(dbManager.isMapLevelAvailable(texture.getMapLevel().getLevel()));
		}
		updateContinueButton();
	}

	@Override
	protected void onResume() {
		updateContinueButton();
		super.onResume();
	}

	@Override
	public boolean onMenuClicked(MENU type) {
		switch (type) {
		case NEW_GAME:
			startGame(selectedMapLevel.getLevel(), true);
			return true;
		case CONTINUE:
			startGame(selectedMapLevel.getLevel(), false);
			return true;
		default:
			return false;
		}
	}

	private Sprite getLevelSelector(MapLevel mapLevel) {
		BitmapTextureAtlas bitmapTextureAtlas = new BitmapTextureAtlas(128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		String imagePath = "level_selector.jpg";

		TextureRegion textureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas,
				this, imagePath, 0, 0);
		getEngine().getTextureManager().loadTexture(bitmapTextureAtlas);

		Sprite selector = new Sprite(mapLevel.getWidth() / 4, mapLevel.getHeight() / 4, mapLevel.getWidth() / 2,
				mapLevel.getHeight() / 2, textureRegion);
		return selector;

	}

	@Override
	protected void pauseGame() {
	}

	@Override
	protected void resumeGame() {
	}

	@Override
	protected void onHardKeyMenuPressed() {
	}
}
